package com.mine.controller.factory;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import java.io.File;

/**
 * 实现播放失败音乐
 */
public class LoseMusic implements Music{
    private Clip clip;

    @Override
    public void play() {
        try {
            File file = new File("src/resource/default/sound/lose.wav");
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
            clip = (Clip) AudioSystem.getLine(new DataLine.Info(Clip.class, audioInputStream.getFormat()));
            clip.open(audioInputStream);
            clip.setFramePosition(0);
            clip.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void stop() {
        if(clip!=null) clip.stop();
    }
}
